package com.javathread.th1;

import com.javathread.thread01.SleepTools;

//volatile 使变量在线程之间是可见的  无法提供操作的原子性
public class VolatileVar  {
	
	private static class VolatitleUn implements Runnable {
		
		private volatile int a = 0;

		@Override
		public void run() {
			String threadName = Thread.currentThread().getName();
			a = a++;
			System.out.println(threadName + "启动:==================a++ a等于" + a);
			SleepTools.ms(100);
			a = a+1;
			System.out.println(threadName + "执行加2:==================a+1 a等于" + a);
		}
		
	}
	
	public static void main(String[] args) {
		//创建一个任务
		VolatitleUn v= new VolatitleUn();
		
		//创建一个循环
//		for (int i=0; i<4 ; i++) {
//			Thread t = new Thread(v);
//			t.start();
//		}
		Thread t1 = new Thread(v);
		Thread t2 = new Thread(v);
		Thread t3 = new Thread(v);
		Thread t4 = new Thread(v);
		
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}
